package com.jming.jike.service.front.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jming.jike.entity.*;
import com.jming.jike.mapper.*;
import com.jming.jike.service.front.ClickMealService;
import com.jming.jike.util.CopyBeanUtils;
import com.jming.jike.vo.backend.DishFlavorVO;
import com.jming.jike.vo.front.CategoryVO;
import com.jming.jike.vo.front.MealDetailVO;
import com.jming.jike.vo.front.MealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/05/01
 */
@Slf4j
@Service
public class ClickMealServiceImpl implements ClickMealService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public List<CategoryVO> categoryList() {

        // 查询菜品分类列表
        // 排序规则 首先按分类排序 菜品 > 套餐
        // 如果分类一样 按排序字段排序
        // 如果排序字段一样 按更新时间排序 最后更新的排在前面;

        return CopyBeanUtils.copyBeanList(categoryMapper.selectList(Wrappers.<Category>lambdaQuery()
                .orderByAsc(Category::getType)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime)), CategoryVO.class);
    }

    @Override
    public List<MealVO> mealList(Long categoryId) {

        // 根据菜品分类查询菜品列表并返回结果
        return CopyBeanUtils.copyBeanList(dishMapper.mealList(categoryId), MealVO.class);
    }

    @Override
    public List<MealDetailVO> mealDetail(Long soleId) {

        // 根据唯一id查询菜品信息
        Dish dish = dishMapper.selectOne(Wrappers.<Dish>lambdaQuery()
                .eq(Dish::getSoleId, soleId).last("limit 1"));

        // 如果菜品信息不为空 则查询口味信息
        if (!ObjectUtils.isEmpty(dish)) {
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(Wrappers.<DishFlavor>lambdaQuery()
                    .eq(DishFlavor::getDishId, dish.getId())
                    .eq(DishFlavor::getIsDeleted, 0).orderByAsc(DishFlavor::getId));

            // 处理数据
            MealDetailVO mealDetailVO = CopyBeanUtils.copyBean(dish, new MealDetailVO());
            mealDetailVO.setDishFlavorVOList(CopyBeanUtils.copyBeanList(dishFlavorList, DishFlavorVO.class));
            List<MealDetailVO> mealDetailVOList = new ArrayList<>();
            mealDetailVOList.add(mealDetailVO);

            // 返回菜品信息
            return mealDetailVOList;
        }

        // 如果菜品信息为空 说明该菜品是套菜, 则查询套餐的菜品详情
        List<Dish> dishList = dishMapper.dishListByMeal(soleId);

        // 处理菜品信息并返回结果
        return dishList.stream().map(dl -> {
            MealDetailVO mealDetailVO = CopyBeanUtils.copyBean(dl, new MealDetailVO());
            // 口味列表
            mealDetailVO.setDishFlavorVOList(CopyBeanUtils.copyBeanList(dishFlavorMapper.selectList(Wrappers.<DishFlavor>lambdaQuery()
                    .eq(DishFlavor::getDishId, dl.getId())
                    .eq(DishFlavor::getIsDeleted, 0)
                    .orderByAsc(DishFlavor::getId)), DishFlavorVO.class));
            return mealDetailVO;
        }).collect(Collectors.toList());
    }
}
